package com.lyh.controller;

import com.lyh.dao.ILogDAO;
import com.lyh.model.Log;
import com.lyh.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Api(description = "日志相关")
@Controller
@RequestMapping("/log")
public class LogController {

	@Autowired
	private ILogDAO mapper;

	@ApiOperation(value = "查询记录", notes = "查询记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping("/user/{name}.json")
	@ResponseBody
	public Object selectUserLogByPage(
			@ApiParam(required = true, value = "用户名") @PathVariable String name,
			@ApiParam(required = true, value = "当前页") @RequestParam("currentPage") int currentPage,
			@ApiParam(required = true, value = "每页显示数量") @RequestParam("pageSize") int pageSize) {
		Page page = new Page();
		page.setLow((currentPage - 1) * pageSize);
		page.setHight(currentPage * pageSize);
		page.setUsername(name);
		List<Log> logs = mapper.selectUserLogByPage(page);
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("status", 0);
		map.put("count", logs.size());
		map.put("list", logs);
		return map;
	}
}
